package com.zyx.spring5msb.aopxml.util;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;

import java.util.Arrays;

/**
 * @author Yaxi.Zhang
 * @since 2021/9/28 14:36
 */
public class LogUtil {
    public static void start(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println("log---" + signature.getName() + "方法开始执行, 参数为:" + Arrays.toString(joinPoint.getArgs()));
    }

    public static void stop(JoinPoint joinPoint, Object result) {
        Signature signature = joinPoint.getSignature();
        System.out.println("log---" + signature.getName() + "方法执行结束, 返回结果:" + result);
    }

    public static void logerror(JoinPoint joinPoint, Exception e) {
        Signature signature = joinPoint.getSignature();
        System.out.println("log---" + signature.getName() + "方法执行异常: " + e.getMessage());
    }

    public static void logfinal(JoinPoint joinPoint) {
        System.out.println("log---方法结束, 执行finally中代码块");
    }

    public Object around(ProceedingJoinPoint pjp) {
        Object result = null;
        Signature signature = pjp.getSignature();
        Object[] args = pjp.getArgs();
        try {
            System.out.println("log---环绕通知start：" + signature.getName() + "方法开始执行了, 参数为:" + Arrays.toString(args));
            result = pjp.proceed(args);
            System.out.println("log---环绕通知stop：" + signature.getName() + "方法开始结束了, 结果为:" + result);
        } catch (Throwable throwable) {
            System.out.println("log---环绕异常通知："+signature.getName()+"出现异常");
            throwable.printStackTrace();
        } finally {
            System.out.println("log---环绕返回通知：" + signature.getName() + "方法返回结果是：" + result);
        }
        return result;
    }

}
